package com.ea.modular.controller.sys;






import java.util.ArrayList;

import java.util.List;


import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


import com.ea.common.annotation.LogAnnotation;
import com.ea.common.node.MenuNode;
import com.ea.common.parent.controller.BaseController;
import com.ea.common.utils.ToolUtil;
import com.ea.modular.model.sys.ShiroUser;

import com.ea.modular.utils.sys.UserUtil;
import com.google.code.kaptcha.Constants;


import io.swagger.annotations.ApiOperation;





/**
 * 登录控制器
 * @author jun
 *
 */
@Controller
public class LoginController extends BaseController {

	/**
	 * 跳转登录页
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/login",method=RequestMethod.GET)
	public String login(HttpServletRequest request,Model model) {


		if (UserUtil.isAuthenticated() || UserUtil.getCurrentUser() != null) {
			return REDIRECT + "/";
		} else {
			//清除系统缓存信息
			UserUtil.clearAllCache();
			return "login";
		}
	}



	/**
	 * Web方式登录走shiro认证
	 * @param username 用户名
	 * @param password 密码,method = RequestMethod.
	 */

	@LogAnnotation
	@ApiOperation(value = "登录应用")
	@RequestMapping(value = "/login",method=RequestMethod.POST)
	public String login(String username, String password,String kaptcha) {

		//验证验证码是否正确

		String code = (String) UserUtil.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
		if (ToolUtil.isEmpty(kaptcha) || !kaptcha.equals(code)) {
			throw new UnknownAccountException("验证码错误");
		}



		UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password);
		SecurityUtils.getSubject().login(usernamePasswordToken);


		return REDIRECT + "/";

	}

	/**
	 * 跳转登录页
	 * @param request
	 * @param model
	 * @return
	 */

	@RequestMapping(value = "/",method=RequestMethod.GET)
	public String home(HttpServletRequest request,Model model){

		//读取菜单信息
		ShiroUser shiroUser = UserUtil.getCurrentUser();
		List<String> roleIds = shiroUser.getRoleList();
		List<MenuNode> menus = new ArrayList<MenuNode>();
		List<MenuNode> titles = new ArrayList<MenuNode>();
		if(ToolUtil.isNotEmpty(roleIds)){
			menus=shiroUser.getMenus();
			titles=MenuNode.buildTitle(menus);
		}



		model.addAttribute("menus", titles);
		model.addAttribute("loginName", shiroUser.getLoginName());
		model.addAttribute("roleName", shiroUser.getRoleName());
		model.addAttribute("user", UserUtil.getUserById(shiroUser.getId()));
		return "index";    
	}


	/**
	 * 跳转登录页
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/sessionout")
	public String logout(HttpServletRequest request,Model model) {
		UserUtil.getSubject().logout();
		model.addAttribute("tips","session过期");
		return "login";
	}


}
